export const getScrollAncestors = (el: HTMLElement): (HTMLElement | Window)[] => {
	const scrollAncestors: (HTMLElement | Window)[] = [];
	let current = el.parentElement;

	// 1. 遍历元素的 DOM 祖先，筛选有滚动能力的容器
	while (current) {
		// 判断容器是否有垂直/水平滚动能力
		const hasVerticalScroll =
			current.scrollHeight > current.clientHeight &&
			['auto', 'scroll'].includes(getComputedStyle(current).overflowY);
		const hasHorizontalScroll =
			current.scrollWidth > current.clientWidth &&
			['auto', 'scroll'].includes(getComputedStyle(current).overflowX);

		if (hasVerticalScroll || hasHorizontalScroll) {
			scrollAncestors.push(current);
		}

		current = current.parentElement;
	}

	// 2. 最后加上 window（页面自身的滚动）
	scrollAncestors.push(window);
	return scrollAncestors;
};
